package fy.uap.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import fy.config.FyConfig;
import fy.util.DBUtil;

public class SupplierXiaDanDao {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(SupplierXiaDanDao.class);

	private String number;

	// private String supplierName;

	private String supplierCode;
	private String pk_corp;
	
	

	public void setPk_corp(String pk_corp) {
		this.pk_corp = pk_corp;
	}

	private List<String> rangeList;

	private List<String> frequencyList;

	public void setSupplierCode(String supplierCode) {
		this.supplierCode = supplierCode;
	}

	public void setRangeList(List<String> rangeList) {
		this.rangeList = rangeList;
	}

	public void setFrequencyList(List<String> frequencyList) {
		this.frequencyList = frequencyList;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public boolean save() {
		boolean success = false;

		Connection conn = null;
		PreparedStatement pst = null;

		try {

			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());

			conn.setAutoCommit(false);

			pst = conn
					.prepareStatement("update apps.FYG_PO_UAP_VENDORS@" +FyConfig.getERP_DB_CONFIG().getDblink()+ " v set v.xdnum = ? where v.vendors_num = ? and v.syb = (select c.unitname from bd_corp c where c.pk_corp = ? and c.dr = 0) and v.uaplb = (select i.cinvclasscode from fy_supplier_invclass i where i.pk_invclass = ? and i.dr = 0)");

			for (int i = 0, length = frequencyList.size(); i < length; i++) {
				pst.setString(1, frequencyList.get(i));
				pst.setString(2, supplierCode);
				pst.setString(3, pk_corp);
				pst.setString(4, rangeList.get(i));
				pst.addBatch();
			}
			pst.executeBatch();
			conn.commit();
			success = true;
			if (!success) {
				throw new Exception();
			}

		} catch (Exception e) {
			success = false;
			DBUtil.rollback(conn);
			e.printStackTrace();
			log.error("《潜在供应商ERP增加下单次数申请表》导入UPA异常，单据编号：" + number, e);
		} finally {
			DBUtil.setAutoCommit(conn, true);

			DBUtil.close(pst, conn);
		}

		return success;
	}

}
